const formatGender = (value: any): number => {
    switch (value) {
        case '男':
            return 1
        case '女':
            return 2
        default:
            return 3
    }
}
const formatAddress = (value: any): string[] => {
    if (value === null || value === undefined) return []

    const result: string[] = value.split(',')
    return result.length === 0 ? [] : result
}
const formatString = (value: any): string => {
    if (value === null || value === undefined) return ''
    return value?.toString() || ''
}
const formatNumber = (value: any): number => {
    if (value === null || value === undefined) return 0
    return Number(value) || 0
}
const formatBoolean = (value: any): boolean => {
    switch (value) {
        case '是':
            return true
        default:
            return false
    }
}
const formatPhoneNumber = (phoneNumber: string) => {
    // 检查输入是否为有效的字符串
    if (typeof phoneNumber !== 'string' || !phoneNumber.match(/^\d{11}$/)) {
        return '电话号码无效'
    }
    // 取出手机号的前三位和后四位
    let firstThree = phoneNumber.substring(0, 3)
    let lastFour = phoneNumber.substring(phoneNumber.length - 4)
    // 中间部分用星号代替
    let middlePart = '*'.repeat(phoneNumber.length - 7)
    // 返回格式化后的手机号
    return `${firstThree}${middlePart}${lastFour}`
}
const formatEmail = (email: string) => {
    // 检查输入是否为有效的字符串
    if (typeof email !== 'string' || !email.includes('@')) {
        return '邮箱无效'
    }
    let atIndex = email.indexOf('@')
    let localPart = email.substring(0, atIndex)
    let domainPart = email.substring(atIndex)
    // 取出手机号的前三位和后四位
    let firstTwo = localPart.substring(0, 2)
    let lastTwo = localPart.substring(localPart.length - 2)
    // 中间部分用星号代替
    let middlePart = '*'.repeat(localPart.length - 4)
    // 返回格式化后的手机号
    return `${firstTwo}${middlePart}${lastTwo}${domainPart}`
}

export { formatBoolean, formatNumber, formatString, formatAddress, formatGender, formatPhoneNumber, formatEmail }
